package com.pk.center.sign.config.esignConfig.scenedemo;

import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.pk.center.sign.config.esignConfig.cn.tsign.hz.comm.EsignHttpHelper;
import com.pk.center.sign.config.esignConfig.cn.tsign.hz.comm.EsignHttpResponse;
import com.pk.center.sign.config.esignConfig.cn.tsign.hz.enums.EsignRequestType;
import com.pk.center.sign.config.esignConfig.cn.tsign.hz.exception.EsignDemoException;
import com.pk.center.sign.config.esignConfig.constant.EsignDemoConfig;
import com.pk.center.sign.config.esignConfig.sign.SignDemo;

import java.util.Map;

/**
 * description 平台方自动签署个人手动签署场景
 *
 * @author 舞乐
 */
class test {
    public static void main(String[] args) throws EsignDemoException {
        String signFlowid = "";

        //下载文档
        //SignDemo.fileDownloadUrl(signFlowid);
        //发起签署
//        SignDemo.createByFile();
    }
}

public class PlatformSignToC {

    private static String eSignHost = EsignDemoConfig.EsignHost;
    private static String eSignAppId = EsignDemoConfig.EsignAppId;
    private static String eSignAppSecret = EsignDemoConfig.EsignAppSecret;
    public static String fileId = "8ace8b2c3f6f4760bb26b3021363752d";
//   流程单号:6cf13a866bb340a4a809a0f533623f30

    public static void main(String[] args) throws EsignDemoException, InterruptedException {
        Gson gson = new Gson();
        //无前置实名，客户需要到签署流程中实名
        //获取文件id以及文件上传地址
//        String filePath = "C:\\Users\\pk\\Desktop\\2境外就业服务合同正式版\\境外就业服务合同（欧洲4.5W）.docx";
//        EsignHttpResponse getUploadUrl = FileDemo.getUploadUrl(filePath);
//        JsonObject getUploadUrlJsonObject = gson.fromJson(getUploadUrl.getBody(), JsonObject.class);
//        JsonObject data = getUploadUrlJsonObject.getAsJsonObject("data");
//        //文件id后续发起签署使用
//        fileId = data.get("fileId").getAsString();
//        String fileUploadUrl = data.get("fileUploadUrl").getAsString();
//        System.out.println("获取文件id以及文件上传地址成功，文件id:" + fileId);
//        System.out.println("上传链接:" + fileUploadUrl);
//
//        //文件上传
//        EsignHttpResponse uploadFileResponse = FileDemo.uploadFile(fileUploadUrl, filePath);
//        JsonObject uploadFileResponseJsonObject = gson.fromJson(uploadFileResponse.getBody(), JsonObject.class);
//        int errCode = uploadFileResponseJsonObject.get("errCode").getAsInt();
//        System.out.println("文件上传成功，状态码:" + errCode);
//
//
//        //文件上传成功后文件会有一个异步处理过程，建议轮询文件状态，正常后发起签署
//        //查询文件上传状态
//        int i = 0;
//        while (i < 3) {
//            EsignHttpResponse fileStatus = FileDemo.getFileStatus(fileId);
//            JsonObject fileStatusJsonObject = gson.fromJson(fileStatus.getBody(), JsonObject.class);
//            JsonObject data1 = fileStatusJsonObject.getAsJsonObject("data");
////            System.out.println("data1---"+data1);
//            String status = fileStatusJsonObject.getAsJsonObject("data").get("fileStatus").getAsString();
//            System.out.println(String.format("查询文件状态执行第%s次", i + 1));
//            if ("2".equalsIgnoreCase(status) || "5".equalsIgnoreCase(status)) {//查询状态为2或者5代表文件准备完成
//                System.out.println("文件准备完成");
//                break;
//            }
//            System.out.println("文件未准备完成,等待两秒重新查询");
//            TimeUnit.SECONDS.sleep(2);
//            i++;
//        }
//        }
        //发起签署
        EsignHttpResponse createByFile = createByFile();
        JSONObject createByFileJsonObject = JSONObject.parseObject(createByFile.getBody());
        String signFlowId = createByFileJsonObject.getJSONObject("data").getString("signFlowId");
        System.out.println("流程单号:" + signFlowId);

//        //获取合同文件签署链接
        SignDemo.signUrl(signFlowId);

//        //下载已签署文件及附属材料
        SignDemo.fileDownloadUrl(signFlowId);

    }


    /**
     * 发起签署
     *
     * @return
     * @throws EsignDemoException
     */
    public static EsignHttpResponse createByFile() throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/create-by-file";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null

        //电话或邮箱
        String tel_or_email = "257987279@qq.com" ;

        //签署位置可移动
        String jsonParm = "{\n" +
                " \"docs\": [\n" +
                "  {\n" +
//                "   \"fileId\": \"9d77059251cb4917a42cf57d120c3986\",\n" +
                "   \"fileId\": \"" + fileId + "\",\n" +
                "   \"fileName\": \"软件销售合同.pdf\"\n" +
                "  }\n" +
                " ],\n" +
                " \"signFlowConfig\": {\n" +
                "  \"signFlowTitle\": \"境外就业合同\",\n" +
                "  \"autoStart\": true,\n" +
                "  \"autoFinish\": true,\n" +
                "  \"noticeConfig\": {\n" +
                "   \"noticeTypes\": \"1,2\"\n" +
                "  },\n" +
                "  \"notifyUrl\": \"回调地址\",\n" +
                "  \"redirectConfig\": {\n" +
                "   \"redirectUrl\": \"https://www.esign.cn/\"\n" +
                "  }\n" +
                " },\n" +
                " \"signers\": [\n" +
                "  {\n" +
                "   \"signConfig\": {\n" +
                "    \"signOrder\": 1\n" +
                "   },\n" +
                "   \"signerType\": 1,\n" +
                "   \"signFields\": [\n" +
                "    {\n" +
                "     \"customBizNum\": \"自定义编码001\",\n" +
                "   \"fileId\": \"" + fileId + "\",\n" +
                "     \"normalSignFieldConfig\": {\n" +
                "      \"autoSign\": true,\n" +
                "      \"signFieldPosition\": {\n" +
                "       \"positionPage\": \"11\",\n" +
                "       \"positionX\": 240,\n" +
                "       \"positionY\": 262\n" +
                "      },\n" +
                "      \"signFieldStyle\": 1\n" +
                "     }\n" +
                "    },\n" +
                "                {\n" +
                "     \"customBizNum\": \"自定义编码002\",\n" +
                "   \"fileId\": \"" + fileId + "\",\n" +
                "     \"normalSignFieldConfig\": {\n" +
                "      \"autoSign\": true,\n" +
                "      \"signFieldPosition\": {\n" +
                "       \"positionPage\": \"13\",\n" +
                "       \"positionX\": 240,\n" +
                "       \"positionY\": 340\n" +
                "      },\n" +
                "      \"signFieldStyle\": 1\n" +
                "     }\n" +
                "                }\n" +
                "   ]\n" +
                "  },\n" +
                "  {\n" +
                "   \"psnSignerInfo\": {\n" +
                "    \"psnAccount\": \""+tel_or_email+"\"\n" +
                "   },\n" +
                "   \"signConfig\": {\n" +
                "    \"forcedReadingTime\": 10,\n" +
                "    \"signOrder\": 2\n" +
                "   },\n" +
                "   \"signerType\": 0,\n" +
                "   \"signFields\": [\n" +
                "                {\n" +
                "     \"customBizNum\": \"自定义编码001\",\n" +
                "   \"fileId\": \"" + fileId + "\",\n" +
                "                    \"signFieldType\":0,\n" +
                "      \"normalSignFieldConfig\": {\n" +
                "                     \"freeMode\":true\n" +
                "      }\n" +
                "                },\n" +
                "                {\n" +
                "     \"customBizNum\": \"自定义编码001\",\n" +
                "   \"fileId\": \"" + fileId + "\",\n" +
                "                    \"signFieldType\":1,\n" +
                "     \n" +
                "                    \"remarkSignFieldConfig\":{\n" +
                "                     \"freeMode\":true,\n" +
                "                     \"inputType\":2\n" +
                "                     \n" +
                "    }\n" +
                "                }\n" +
                "   ]\n" +
                "  }\n" +
                " ]\n" +
                "}";

         //   签署位置固定
        /*String jsonParm ="{\n" +
                "\"docs\": [\n" +
                "{\n" +
                "\"fileId\": \"c08980f36a2745958c8ec44f4318bb84\",\n" +
                "\"fileName\": \"请设置待签署文件的文件名称.pdf\"\n" +
                "}\n" +
                "],\n" +
                "\"signFlowConfig\": {\n" +
                "\"signFlowTitle\": \"请设置当前签署任务的主题：此场景演示平台自身、个人用户双方签署\",\n" +
                "\"autoStart\": true,\n" +
                "\"autoFinish\": true,\n" +
                "\"noticeConfig\": {\n" +
                "\"noticeTypes\": \"1,2\"\n" +
                "},\n" +
                "\"notifyUrl\": \"请设置异步回调地址，以http开始\",\n" +
                "\"redirectConfig\": {\n" +
                "\"redirectUrl\": \"https://www.esign.cn/\"\n" +
                "}\n" +
                "},\n" +
                "\"signers\": [\n" +
                "{\n" +
                "\"signConfig\": {\n" +
                "\"signOrder\": 1\n" +
                "},\n" +
                "\"signerType\": 1,\n" +
                "\"signFields\": [\n" +
                "{\n" +
                "\"customBizNum\": \"甲方001\",\n" +
                "\"fileId\": \"c08980f36a2745958c8ec44f4318bb84\",\n" +
                "\"normalSignFieldConfig\": {\n" +
                "\"autoSign\": true,\n" +
                "\"signFieldPosition\": {\n" +
                "\"positionPage\": \"11\",\n" +
                "\"positionX\": 240,\n" +
                "\"positionY\": 262\n" +
                "},\n" +
                "\"signFieldStyle\": 1\n" +
                "}\n" +
                "},\n" +
                "                {\n" +
                "\"customBizNum\": \"甲方002\",\n" +
                "\"fileId\": \"c08980f36a2745958c8ec44f4318bb84\",\n" +
                "\"normalSignFieldConfig\": {\n" +
                "\"autoSign\": true,\n" +
                "\"signFieldPosition\": {\n" +
                "\"positionPage\": \"13\",\n" +
                "\"positionX\": 240,\n" +
                "\"positionY\": 340\n" +
                "},\n" +
                "\"signFieldStyle\": 1\n" +
                "}\n" +
                "}\n" +
                "]\n" +
                "},\n" +
                "{\n" +
                "\"psnSignerInfo\": {\n" +
                "\"psnAccount\": \"2579874279@qq.com\"\n" +
                "},\n" +
                "\"signConfig\": {\n" +
                "\"forcedReadingTime\": 10,\n" +
                "\"signOrder\": 2\n" +
                "},\n" +
                "\"signerType\": 0,\n" +
                "\"signFields\": [\n" +
                "{\n" +
                "\"customBizNum\": \"乙方001\",\n" +
                "\"fileId\": \"c08980f36a2745958c8ec44f4318bb84\",\n" +
                "\"normalSignFieldConfig\": {\n" +
                "\"signFieldPosition\": {\n" +
                "\"positionPage\": \"2\",\n" +
                "\"positionX\": 416,\n" +
                "\"positionY\": 75\n" +
                "},\n" +
                "\"signFieldStyle\": 1\n" +
                "}\n" +
                "},\n" +
                "                {\n" +
                "\"customBizNum\": \"乙方002\",\n" +
                "\"fileId\": \"c08980f36a2745958c8ec44f4318bb84\",\n" +
                "\"normalSignFieldConfig\": {\n" +
                "\"signFieldPosition\": {\n" +
                "\"positionPage\": \"11\",\n" +
                "\"positionX\": 508,\n" +
                "\"positionY\": 276\n" +
                "},\n" +
                "\"signFieldStyle\": 1\n" +
                "}\n" +
                "},\n" +
                "                {\n" +
                "\"customBizNum\": \"乙方003\",\n" +
                "\"fileId\": \"c08980f36a2745958c8ec44f4318bb84\",\n" +
                "\"normalSignFieldConfig\": {\n" +
                "\"signFieldPosition\": {\n" +
                "\"positionPage\": \"13\",\n" +
                "\"positionX\": 516,\n" +
                "\"positionY\": 337\n" +
                "},\n" +
                "\"signFieldStyle\": 1\n" +
                "}\n" +
                "}\n" +
                "]\n" +
                "}\n" +
                "]\n" +
                "}";
        */

        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成请求签名鉴权方式的Header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);


        // 发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }
}
